package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.mapper.MenuMapper;
import com.itheima.mapper.PermissionMapper;
import com.itheima.mapper.PointMapper;
import com.itheima.pojo.PePermission;
import com.itheima.pojo.PePermissionMenuExtend;
import com.itheima.pojo.PePermissionPointExtend;
import com.itheima.service.MenuService;
import com.itheima.util.ChildsDto;
import com.itheima.util.MenuAdd;
import com.itheima.util.MenuDto;
import com.itheima.util.PointDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private PointMapper pointMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    //菜单表列表查询  权限点集合挂在菜单项上 ChildDto为所有业务
    @Override
    public List<ChildsDto> list() {
        LambdaQueryWrapper<PePermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(PePermission::getPid);
        List<PePermission> pePermissions = permissionMapper.selectList(wrapper);
        ArrayList<ChildsDto> list = new ArrayList<>();
        //查询框架项目
        for (PePermission pePermission : pePermissions) {
            ChildsDto childsDto = new ChildsDto();
            Long id = pePermission.getId();
            LambdaQueryWrapper<PePermission> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(PePermission::getPid,id).isNotNull(PePermission::getPermissionMenuExtendId);
            List<PePermission> pePermissions1 = permissionMapper.selectList(wrapper1);
            //查菜单项
            ArrayList<MenuDto> menuDtos = new ArrayList<>();
            for (PePermission permission : pePermissions1) {
                MenuDto menuDto = new MenuDto();
                Long id1 = permission.getId();
                LambdaQueryWrapper<PePermission> wrapper2 = new LambdaQueryWrapper<>();
                wrapper2.eq(PePermission::getPid,id1).isNotNull(PePermission::getPermissionPointExtendId);
                List<PePermission> pePermissions2 = permissionMapper.selectList(wrapper2);
                //查权限项
                ArrayList<PointDto> pointDtos = new ArrayList<>();
                for (PePermission pePermission1 : pePermissions2) {
                    PointDto pointDto = new PointDto();
                    Long id2 = pePermission1.getId();
                    LambdaQueryWrapper<PePermissionPointExtend> wrapper3 = new LambdaQueryWrapper<>();
                    wrapper3.eq(PePermissionPointExtend::getPermissionId,id2);
                    PePermissionPointExtend point = pointMapper.selectOne(wrapper3);
                    pointDto.setCode(point.getCode());
                    pointDto.setId(pePermission1.getId());
                    pointDto.setPid(pePermission1.getPid());
                    pointDto.setTitle(pePermission1.getName());
                    pointDtos.add(pointDto);
                }
                menuDto.setPoints(pointDtos);
                LambdaQueryWrapper<PePermissionMenuExtend> wrapper3 = new LambdaQueryWrapper<>();
                wrapper3.eq(PePermissionMenuExtend::getPermissionId,id1);
                PePermissionMenuExtend menu = menuMapper.selectOne(wrapper3);
                menuDto.setId(permission.getId());
                menuDto.setPid(permission.getPid());
                menuDto.setCode(menu.getCode());
                menuDto.setTitle(permission.getName());
                menuDtos.add(menuDto);
            }
            childsDto.setId(id);
            LambdaQueryWrapper<PePermissionMenuExtend> wrapper4 = new LambdaQueryWrapper<>();
            wrapper4.eq(PePermissionMenuExtend::getPermissionId,id);
            PePermissionMenuExtend menu = menuMapper.selectOne(wrapper4);
            childsDto.setCode(menu.getCode());
            childsDto.setChilds(menuDtos);
            childsDto.setTitle(pePermission.getName());
            list.add(childsDto);
        }
        return list;
    }

    //添加菜单项   可能为权限项
    @Override
    public void add(MenuAdd menuAdd) {
        PePermission pePermission = new PePermission();
        //添加许可方式
        if(menuAdd.getPid()!=null && menuAdd.getPid()!=0)
        pePermission.setPid(menuAdd.getPid());
        pePermission.setCreateTime(new Date());
        pePermission.setName(menuAdd.getTitle());
        permissionMapper.insert(pePermission);
        //添加权限项
        if(menuAdd.getIsPoint()){
            pePermission.setType(2);
            PePermissionPointExtend pointExtend = new PePermissionPointExtend();
            pointExtend.setCode(menuAdd.getCode());
            pointExtend.setPermissionId(pePermission.getId());
            pointMapper.insert(pointExtend);
            pePermission.setPermissionPointExtendId(pointExtend.getId());
            permissionMapper.updateById(pePermission);
            //否则添加菜单项
        }else {
            pePermission.setType(1);
            PePermissionMenuExtend menuExtend = new PePermissionMenuExtend();
            menuExtend.setCode(menuAdd.getCode());
            menuExtend.setPermissionId(pePermission.getId());
            menuMapper.insert(menuExtend);
            pePermission.setPermissionMenuExtendId(menuExtend.getId());
            permissionMapper.updateById(pePermission);
        }
    }


    //根据Id查菜单项相关
    @Override
    public MenuAdd selById(Long id) {
        PePermission pePermission = permissionMapper.selectById(id);
        MenuAdd menuAdd = new MenuAdd();
        menuAdd.setPid(pePermission.getPid());
        menuAdd.setId(Integer.parseInt(id+""));
        menuAdd.setTitle(pePermission.getName());
        //如果是菜单项
        if(pePermission.getPermissionMenuExtendId()!=null){
            menuAdd.setIsPoint(false);
            LambdaQueryWrapper<PePermissionMenuExtend> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PePermissionMenuExtend::getPermissionId,id);
            PePermissionMenuExtend menuExtend = menuMapper.selectOne(wrapper);
            menuAdd.setCode(menuExtend.getCode());
        }
        //如果是权限项
        if(pePermission.getPermissionPointExtendId()!=null){
            menuAdd.setIsPoint(true);
            LambdaQueryWrapper<PePermissionPointExtend> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PePermissionPointExtend::getPermissionId,id);
            PePermissionPointExtend pointExtend = pointMapper.selectOne(wrapper);
            menuAdd.setCode(pointExtend.getCode());
        }
        return menuAdd;
    }

    //根据Id删除相关项
    @Override
    public void delById(Long id) {
        PePermission pePermission = permissionMapper.selectById(id);
        if (pePermission.getPermissionPointExtendId() != null) {
            LambdaQueryWrapper<PePermissionPointExtend> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PePermissionPointExtend::getPermissionId, id);
            pointMapper.delete(wrapper);
        }
        if (pePermission.getPermissionMenuExtendId() != null) {
            LambdaQueryWrapper<PePermissionMenuExtend> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PePermissionMenuExtend::getPermissionId, id);
            menuMapper.delete(wrapper);
        }
        permissionMapper.deleteById(id);
        LambdaQueryWrapper<PePermission> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(PePermission::getPid,id);
        List<PePermission> pePermissions = permissionMapper.selectList(wrapper1);
        if(pePermissions!=null){
        for (PePermission permission : pePermissions) {
            delById(permission.getId());
        }
        }else {
            return;
        }
    }

    @Override
    public void upById(Long id, MenuAdd menuAdd) {
        System.out.println(menuAdd);
        PePermission pePermission = permissionMapper.selectById(id);
        //修改许可方式
        if(menuAdd.getPid()!=null)
            pePermission.setPid(menuAdd.getPid());
        pePermission.setName(menuAdd.getTitle());
        permissionMapper.updateById(pePermission);
        //修改权限项
        if(menuAdd.getIsPoint()){
            pePermission.setType(2);
            LambdaUpdateWrapper<PePermissionPointExtend> wrapper = new LambdaUpdateWrapper<>();
            wrapper.set(PePermissionPointExtend::getCode,menuAdd.getCode()).eq(PePermissionPointExtend::getPermissionId,id);
            pointMapper.update(null,wrapper);
            //否则修改菜单项
        }else {
            pePermission.setType(1);
            LambdaUpdateWrapper<PePermissionMenuExtend> wrapper = new LambdaUpdateWrapper<>();
            wrapper.set(PePermissionMenuExtend::getCode,menuAdd.getCode()).eq(PePermissionMenuExtend::getPermissionId,id);
            menuMapper.update(null,wrapper);
        }
    }
}
